<?php

require_once("JSON.php");

include("../common/common.php");

$restoid=null;
if(isset($_GET["restoid"])){
	$restoid=$_GET["restoid"];
}

$_lang = null;
if (is_null($_lang) and isset($_COOKIE['_lang'])){
	$_lang=$_COOKIE['_lang'];
}else{
	if(is_null($_lang)){
		$_lang = FR;
	}
}

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

/// Connect to server and select databse.
	// Connect to server and select databse.
	$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
	$db->connect();

	// Select the rows in the  table
	$query = "SELECT info_resto.RESTO_NAME_$_lang AS name, 
		info_resto.RESTO_ADDR AS address 
		 FROM info_resto WHERE info_resto.RESTO_ID=$restoid";
	$result = $db->query_first($query);
	if (!$result) {
	  die('Invalid query: ' . mysql_error());
	}
	$db->close();

header("Content-type: text/xml");



// Iterate through the rows, printing XML nodes for each

	$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($result['address']).'&sensor=false');
	// We convert the JSON to an array
	$json = new Services_JSON();
	$geo = get_object_vars($json->decode($geo));
// If everything is cool
print_r($geo);
	if ($geo['status'] == 'OK') {
	// We set our values
	$results = get_object_vars($geo['results'][0]);
	$geometry = get_object_vars($results['geometry']);
	$location = get_object_vars($geometry['location']);
	$latitude = $location['lat'];
	$longitude = $location['lng'];
// 		$latitude = $geo['results'][0]['geometry']['location']['lat'];
// 		$longitude = $geo['results'][0]['geometry']['location']['lng'];
	}
  // ADD TO XML DOCUMENT NODE
  echo '<markers>';
  echo '<marker ';
  echo 'name="' . parseToXML($result['name']) . '" ';
  echo 'address="' . parseToXML($result['address']) . '" ';
  echo 'lat="' . $latitude . '" ';
  echo 'lng="' . $longitude . '" ';
  echo 'type="' . "bar" . '" ';
  echo '/>';
  // End XML file
echo '</markers>';

?>
